﻿Imports TyranntEngine

Public Class WelcomeDatabaseAccess

    Private db As New TyranntWelcomeDatasourceDataContext
    Private _naz As New Nabaztag
    Private _lastError As Exception
    Private _causedError As Boolean = False

    Public Function GetClientFiles() As String
        Try
            Dim result = (From file In db.files _
                          Select file).ToList

            Dim filesXML = <files>
                               <%= From file In result _
                                   Select <file id=<%= file.fileID %>>
                                              <name><%= file.filename %></name>
                                              <size><%= file.size.ToString %></size>
                                              <hash><%= file.hashcode %></hash>
                                          </file> %>
                           </files>
            Return filesXML.ToString
        Catch ex As Exception
            _lastError = ex
            Return Failed("GetClientFiles")
        End Try
    End Function

    Public Function AddFile(ByVal filename As String, ByVal hashcode As String, ByVal size As Long) As String
        If CheckFileExists(filename) = True Then
            Return UpdateFile(filename, hashcode)
        Else
            If _causedError = True Then
                Return Failed("CheckFileExists")
            End If
        End If
        Try
            Dim aFile As New file
            aFile.filename = filename
            aFile.hashcode = hashcode
            aFile.size = size
            db.files.InsertOnSubmit(aFile)
            db.SubmitChanges()
            Return ""
        Catch ex As Exception
            _lastError = ex
            Return Failed("AddFile")
        End Try
    End Function

    Public Function DeleteFile(ByVal filename As String) As String
        If CheckFileExists(filename) = False Then
            If _causedError = True Then
                Return Failed("CheckFileExists")
            Else
                Return "[#]filenotexist"
            End If
        End If

        Dim aFile As file = PullFile(filename)
        If IsNothing(aFile) = True Then
            Return Failed("PullFile")
        Else
            If aFile.filename = "" Then
                Return "[#]filenotexist[PullFile]"
            End If
        End If
        Try
            db.files.DeleteOnSubmit(aFile)
            db.SubmitChanges()
            Return ""
        Catch ex As Exception
            _lastError = ex
            Return Failed("DeleteFile")
        End Try
    End Function

    Private Function UpdateFile(ByVal filename As String, ByVal hashcode As String) As String
        Dim aFile As file = PullFile(filename)
        aFile.hashcode = hashcode
        Try
            db.SubmitChanges()
            Return ""
        Catch ex As Exception
            _lastError = ex
            Return Failed("UpdateFile")
        End Try
    End Function

    Private Function PullFile(ByVal filename As String) As file
        Try
            Dim result = (From file In db.files _
                                 Where file.filename = filename _
                                 Select file).ToList
            If result.Count = 0 Then
                Return New file
            Else
                Return result(0)
            End If
        Catch ex As Exception
            _lastError = ex
            Return Nothing
        End Try

    End Function

    Private Function CheckFileExists(ByVal filename As String) As Boolean
        _causedError = False
        Try
            Dim result = (From file In db.files _
                          Where file.filename = filename _
                          Select file).ToList
            If result.Count = 0 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            _lastError = ex
            _causedError = True
            Return False
        End Try
    End Function

    Public Function PreloadFile(ByVal filename As String, ByVal size As Long) As String
        Try
            Dim result = (From preFile In db.filePreloads _
                          Select preFile).ToList
            If result.Count > 0 Then
                Return "[#]alreadywaiting[" + result(0).preloadFilename + "]"
            Else
                Dim preFile As New filePreload
                preFile.preloadFilename = filename
                preFile.filesize = size
                db.filePreloads.InsertOnSubmit(preFile)
                db.SubmitChanges()
                Return ""
            End If
        Catch ex As Exception
            _lastError = ex
            Return Failed("PreloadFile")
        End Try
    End Function

    Public Function PullPreloadFile() As FileDetails
        Try
            Dim result = (From preFile In db.filePreloads _
                          Select preFile).ToList
            If result.Count = 0 Then
                Dim file As New FileDetails
                file.Filename = "[#]noprefile"
                Return file
            Else
                Dim file As New FileDetails
                file.Filename = result(0).preloadFilename
                file.FileSize = result(0).filesize
                db.filePreloads.DeleteOnSubmit(result(0))
                db.SubmitChanges()
                Return file
            End If
        Catch ex As Exception
            _lastError = ex
            Dim file As New FileDetails
            file.Filename = Failed("PullPreloadFile()")
            Return file
        End Try
    End Function

    Public Function Failed(ByVal routine As String) As String
        _naz.SendMessage("fatal error", "Fatal error in routine " + routine + ". Exception caught!", Nabaztag.VoicesEnum.UK_Edwin)
        Return "[#]failed[" + routine + "]|" + _lastError.ToString
    End Function
End Class

Public Class FileDetails
    Private _filename As String = ""
    Private _size As Long = 0

    Public Property Filename() As String
        Get
            Return _filename
        End Get
        Set(ByVal value As String)
            _filename = value
        End Set
    End Property

    Public Property FileSize() As Long
        Get
            Return _size
        End Get
        Set(ByVal value As Long)
            _size = value
        End Set
    End Property
End Class